// pages/editPartTime/editPartTime.js
var app = getApp()
const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
const wxMap = new QQMapWX({
  key: 'AB3BZ-QEI6J-7SAFK-FV2J4-YEAR7-C7FYZ' // 必填
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    id:'',
    array: ['酒店', '活动', '群演', '安保', '礼仪', '发单', '话务', '工厂', '充场', '家教', '其他'],
    index: '',
    jiesuan: ['日结', '月结'],
    jsindex: '',
    mapShow: false,
    fontcolor: '#000',
    colors: [],
    startDate: '',
    startdetailDate: '',
    endDate: '',
    enddetailDate: '',
    lastDate: '',
    lastdetailDate: '',
    region: "山东省青岛市黄岛区",
    customItem: '全部',
    sex:0,
    hide:10,

    type: 1, //兼职
    job_type: 1, //兼职类型*
    title: '', //兼职名称
    color: 0, //颜色
    latitude: 0,
    longitude: 0,
    ad_type: '',  //行政区级别*
    ad_code: null,  //行政区区划码*
    people_num: '',  //兼职人数
    gender_needed: 0, //姓别要求
    sign_end: null, //报名截止时间
    work_start: null, //工作开始时间
    worl_end: null, //工作结束时间
    salary: '', //薪资水平
    pay_typed: '', //结算类型
    content: '', //详情
    contact_name: '',//联系人
    contact_phone: '',//联系人电话
  },
  timeto: function (dated, time ='00:00:00'){
    var date = dated + ' ' + time;
    console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    console.log(String(timestamp))
    return String(timestamp)
  },
  //获取兼职信息
  getPartTimeInfo: function (id) {
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/part-times/' + id, // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res.data, '单个兼职000')
        // this.timeto(res.data.start_at, res.data.work_start)
        if (res.data.work_start == null){
          var workStart =this.timeto(res.data.start_at, '00:00:00')
        }else{
          var workStart = this.timeto(res.data.start_at, res.data.work_start)
        }
        if (res.data.work_end == null) {
          var workEnd = this.timeto(res.data.end_at, '00:00:00')
        } else {
          var workEnd = this.timeto(res.data.end_at, res.data.work_end)
        }
        if (res.data.end_at == '1970-01-01'){
          var endAt = null
        }else{
          var endAt = res.data.end_at
        }
        this.setData({
          title: res.data.title,
          fontcolor: res.data.color,
          region: res.data.location,
          ad_code: res.data.ad_code,
          people_num: res.data.people_num,
          sex: res.data.gender_needed,
          gender_needed: res.data.gender_needed,
          lastDate: res.data.sign_end,
          startDate: res.data.start_at,
          endDate: endAt,
          startdetailDate: res.data.work_start,
          enddetailDate: res.data.work_end,
          sign_end: this.timeto(res.data.sign_end, '00:00'), //报名截止时间
          work_start: workStart, //工作开始时间
          worl_end: workEnd, //工作结束时间
          salary: res.data.salary,
          jsindex: res.data.pay_type - 1,
          pay_typed: res.data.pay_type,  
          content: res.data.content,
          contact_name: res.data.contact_name,
          contact_phone: res.data.contact_phone,
          hide: res.data.hide
        })
        for (let i = 0; i < this.data.array.length; i++){
          let item = this.data.array[i]
          if (item.id == res.data.job_type){
            this.setData({
              index: i,
              job_type:item.id
            })
            break
          }
        }
        for (let i = 0; i < this.data.colors.length; i++) {
          let item = this.data.colors[i]
          if (item.color == res.data.color) {
            this.setData({
              color: item.id,
            })
            break
          }
        }
        if (res.data.latitude != null && res.data.latitude != ''){
         this.setData({
           latitude: res.data.latitude,
           longitude: res.data.longitude,
              markers: [{
                iconPath: '/img/fabu/position.png',
                id: 0,
                latitude: res.data.latitude,
                longitude: res.data.longitude,
                width: 30,
                height: 30
              }],
              controls: [{
                id: 1,
                iconPath: '/img/fabu/position.png',
                position: {
                  left: 0,
                  top: 0,
                  width: 30,
                  height: 30
                },
                clickable: true
              }]
            })
        } else if (res.data.latitude == null || res.data.latitude == ''){
              wx.getLocation({
                type: 'wgs84',
                success: (res) => {
                  this.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    markers: [{
                      iconPath: '/img/fabu/position.png',
                      id: 0,
                      latitude: res.latitude,
                      longitude: res.longitude,
                      width: 30,
                      height: 30
                    }],
                    controls: [{
                      id: 1,
                      iconPath: '/img/fabu/position.png',
                      position: {
                        left: 0,
                        top: 0,
                        width: 30,
                        height: 30
                      },
                      clickable: true
                    }]
                  })
                }
              })
        }
       
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })

  },
  validate: function () {
    // if (this.data.job_type == 0) {
    //   wx.showToast({
    //     title: '选择兼职类型',
    //   })
    //   return false
    // }
    // if (this.data.title == '') {
    //   wx.showToast({
    //     title: '填写兼职名称',
    //   })
    //   return false
    // }
    // if (this.data.ad_code == '') {
    //   wx.showToast({
    //     title: '选择具体位置',
    //   })
    //   return false
    // }
    // if (this.data.people_num == 0) {
    //   wx.showToast({
    //     title: '填写兼职人数',
    //   })
    //   return false
    // }
    // if (this.data.lastDate == '') {
    //   wx.showToast({
    //     title: '选择截止日期',
    //   })
    //   return false
    // }
    // if (this.data.lastdetailDate == '') {
    //   wx.showToast({
    //     title: '选择截止时间',
    //   })
    //   return false
    // }
    // if (this.data.startDate == '') {
    //   wx.showToast({
    //     title: '选择开始日期',
    //   })
    //   return false
    // }
    // if (this.data.endDate == '') {
    //   wx.showToast({
    //     title: '选择结束日期',
    //   })
    //   return false
    // }
    // if (this.data.startdetailDate == '') {
    //   wx.showToast({
    //     title: '选择上班时间',
    //   })
    //   return false
    // }
    // if (this.data.enddetailDate == '') {
    //   wx.showToast({
    //     title: '选择下班时间',
    //   })
    //   return false
    // }
    // if (this.data.salary == '') {
    //   wx.showToast({
    //     title: '填写薪资水平',
    //   })
    //   return false
    // }
    // if (this.data.pay_typed == '') {
    //   wx.showToast({
    //     title: '选择结算类型',
    //   })
    //   return false
    // }
    // if (this.data.content == '') {
    //   wx.showToast({
    //     title: '填写兼职详情',
    //   })
    //   return false
    // }
    // if (this.data.contact_name == '') {
    //   wx.showToast({
    //     title: '填写联系人',
    //   })
    //   return false
    // }
    if (this.data.contact_phone != '' && this.data.contact_phone != null && this.data.contact_phone !='null') {
      console.log(this.data.contact_phone);
      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (!myreg.test(this.data.contact_phone)) {
        wx.showToast({
          title: '手机号有误！',
          icon: 'none',
          duration: 1500
        });
        return false;
      }
    }
    return true;
  },
  //  取消发布
  delrealse:function(){
    // if (this.data.hide == 20){
    //   this.setData({
    //     hide: 10
    //   })
    //   console.log(this.data.hide);
    // }else 
    if (this.data.hide == 10) {
      this.setData({
        hide: 20
      })
      console.log(this.data.hide);
      this.submitinfo();
    }
  },
  gotorlespt:function(){
    wx.navigateTo({
      url: '/pages/releasePartTime/releasePartTime',
    });
  },
  //提交
  submitinfo: function () {
    // console.log('兼职类型', this.data.job_type, '兼职名称', this.data.title, '详情', this.data.content, '联系人', this.data.contact_name, '联系人电话', this.data.contact_phone, '兼职人数', this.data.people_num, '报名截止时间', this.data.sign_end, '兼职开始时间', this.data.work_start, '兼职结束时间', this.data.worl_end, '工作开始时间', this.data.startdetailDate, '工作结束时间', this.data.enddetailDate, '经纬度', String(this.data.latitude), '经纬度', String(this.data.longitude), '行政区区划码', this.data.ad_code, '薪资水平', this.data.salary, '姓别要求', this.data.gender_needed, '结算类型', this.data.pay_typed,'颜色', this.data.color,'取消发布', this.data.hide,)
    if(this.validate()){
      wx.showLoading({
        title: '请稍等',
      })
      var accesstoken = wx.getStorageSync('accesstoken');
      if (this.data.color != 0) {
        wx.request({
          url: 'https://api.tianlala.com.cn/part-times/' + this.data.id, // 仅为示例
          data: {
            // type: this.data.type, //兼职
            job_type: this.data.job_type, //兼职类型*
            title: this.data.title, //兼职名称
            content: this.data.content, //详情
            contact_name: this.data.contact_name, //联系人
            contact_phone: this.data.contact_phone, //联系人电话
            people_num: this.data.people_num,   //兼职人数
            sign_end: this.data.sign_end,  //报名截止时间
            start_at: this.data.work_start,  //兼职开始时间(时间戳)
            end_at: this.data.worl_end,  //兼职结束时间(时间戳)
            work_start: this.data.startdetailDate,  //工作开始时间
            work_end: this.data.enddetailDate,  //工作结束时间
            latitude: String(this.data.latitude),
            longitude: String(this.data.longitude),
            ad_code: this.data.ad_code,   //行政区区划码*
            // ad_type: this.data.ad_type,   //行政区级别*
            salary: this.data.salary,  //薪资水平
            gender_needed: this.data.gender_needed,  //姓别要求
            pay_type: this.data.pay_typed,  //结算类型
            good_id: this.data.color,  //颜色
            hide: this.data.hide,
          },
          method:'PUT',
          header: {
            // 'content-type': 'application/json', // 默认值
            'Authorization': accesstoken
          },
          success(res) {
            console.log(res)
            wx.hideLoading()
            if (res.statusCode == 200) {
              wx.showToast({
                title: '操作成功',
              })
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }, 2000)
            }
          },
          fail(res){
            console.log(res)
          }
        })
      } else {
        console.log("+6666")
        wx.request({
          url: 'https://api.tianlala.com.cn/part-times/' + this.data.id, // 仅为示例
          data: {
            // type: this.data.type, //兼职
            job_type: this.data.job_type, //兼职类型*
            title: this.data.title, //兼职名称
            content: this.data.content, //详情
            contact_name: this.data.contact_name, //联系人
            contact_phone: this.data.contact_phone, //联系人电话
            people_num: this.data.people_num,   //兼职人数
            sign_end: this.data.sign_end,  //报名截止时间
            start_at: this.data.work_start,  //兼职开始时间(时间戳)
            end_at: this.data.worl_end,  //兼职结束时间(时间戳)
            work_start: this.data.startdetailDate,  //工作开始时间
            work_end: this.data.enddetailDate,  //工作结束时间
            latitude: String(this.data.latitude),
            longitude: String(this.data.longitude),
            ad_code: this.data.ad_code,   //行政区区划码*
            // ad_type: this.data.ad_type,   //行政区级别*
            salary: this.data.salary,  //薪资水平
            gender_needed: this.data.gender_needed,  //姓别要求
            pay_type: this.data.pay_typed,  //结算类型
            // good_id: this.data.color,  //颜色
            hide: this.data.hide,
          },
          method: 'PUT',
          header: {
            // 'content-type': 'application/json', // 默认值
            'Authorization': accesstoken
          },
          success(res) {
            console.log(res)
            wx.hideLoading();
            if (res.statusCode == 200) {
              wx.showToast({
                title: '操作成功',
              })
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }, 2000)
            }
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    }


  },
  // 兼职类型
  bindduolieChange: function (e) {
    // console.log(e.detail.value)
    // console.log(this.data.array[e.detail.value].id)
    this.setData({
      index: e.detail.value,
      job_type: this.data.array[e.detail.value].id
    })
  },

  //兼职名称
  jianzhiname: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  //选择颜色
  fontColorChange: function (e) {
    console.log(e.currentTarget.dataset.colorid)
    this.setData({
      fontcolor: e.currentTarget.dataset.color,
      color: e.currentTarget.dataset.colorid
    })
  },


  regionchange(e) {
    // console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  //选择区
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
    })
  },
  //选择具体位置
  selectdetailadress: function () {
    const that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log(res)
        wxMap.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude,
          },
          success: function (res) {
            console.log(res.result.ad_info, '逆地址解析');
            that.setData({
              ad_code: res.result.ad_info.adcode
            })
          },
        });

        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            iconPath: '/img/fabu/position.png',
            id: 0,
            latitude: res.latitude,
            longitude: res.longitude,
            width: 30,
            height: 30
          }],
          controls: [{
            id: 1,
            iconPath: '/img/fabu/position.png',
            position: {
              left: 0,
              top: 0,
              width: 30,
              height: 30
            },
            clickable: true
          }],
          mapShow: true
        })
      }
    })
  },
  //兼职人数
  jianzhinum: function (e) {
    this.setData({
      people_num: e.detail.value
    })
  },
  //性别要求
  xingbieyaoqiu: function (e) {
    this.setData({
      gender_needed: e.detail.value
    })
  },
  //截止时间
  bindlastDateChange: function (e) {
    this.setData({
      lastDate: e.detail.value
    })
    var date = this.data.lastDate + ' 00:00:00';
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    this.setData({
      sign_end: String(timestamp)
    })
  },
  bindlastdetailDate: function (e) {
    this.setData({
      lastdetailDate: e.detail.value
    })
    var date = this.data.lastDate + ' ' + this.data.lastdetailDate;
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    this.setData({
      sign_end: String(timestamp)
    })
  },
  //开始时间
  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
    var date = this.data.startDate + ' 00:00:00';
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    this.setData({
      work_start: String(timestamp)
    })
  },
  bindstartdetailDate: function (e) {
    this.setData({
      startdetailDate: e.detail.value
    })
    var date = this.data.startDate + ' ' + this.data.startdetailDate;
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    this.setData({
      work_start: String(timestamp)
    })
  },
  //结束时间
  bindEndDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    })
    var date = this.data.endDate + ' 00:00:00';
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp,"结束日期");
    this.setData({
      worl_end: String(timestamp)
    })
  },
  bindenddetailDate: function (e) {
    this.setData({
      enddetailDate: e.detail.value
    })
    var date = this.data.endDate + ' ' + this.data.enddetailDate;
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    this.setData({
      worl_end: String(timestamp)
    })
  },
  //薪资
  jianzhimoeny: function (e) {
    var value = e.detail.value.replace(/[^\d]/g, '')
    // console.log(value)
    this.setData({
      salary: value
    })
  },

  //结算类型
  bindjiesuanChange: function (e) {
    // console.log(e.detail.value)
    var paytyped = 1
    if (e.detail.value == 0) {
      paytyped = 1
    } else if (e.detail.value == 1) {
      paytyped = 2
    }
    // console.log(paytyped)
    this.setData({
      jsindex: e.detail.value,
      pay_typed: paytyped
    })
  },
  textdetail: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  lianxi: function (e) {
    this.setData({
      contact_name: e.detail.value
    })
  },
  phone: function (e) {
    this.setData({
      contact_phone: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl,
      id: options.id
    })
    var accesstoken = wx.getStorageSync('accesstoken')
    if (!this.data.colors[0]) {
      wx.request({
        url: 'https://api.tianlala.com.cn/colors', // 仅为示例
        data: {
        },
        method: 'GET',
        header: {
          //  'content-type': 'application/json', // 默认值
          'Authorization': accesstoken
        },
        success: (res) => {
          console.log(res, '颜色')
          this.setData({
            colors: res.data
          })
        }
      })
    }
    if (true) {
      wx.request({
        url: 'https://api.tianlala.com.cn/job-types/job', // 仅为示例
        data: {
        },
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': accesstoken
        },
        success: (res) => {
          console.log(res, '类型')
          this.setData({
            array: res.data
          })
        }
      })
    }
    this.getPartTimeInfo(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})